package com.hugh.lagouserviceuser.service.impl;

import com.hugh.lagou.user.LagouTokenService;
import com.hugh.lagou.user.entity.LagouTokenEntity;
import com.hugh.lagouserviceuser.dao.LagouTokenDao;
import com.hugh.lagouserviceuser.pojo.LagouToken;
import com.hugh.lagou.user.util.Md5;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Objects;
import java.util.Optional;

/**
 * @Author: Hugh
 * @Date: 2020/7/19
 */
@Slf4j
@RefreshScope
@Service(interfaceClass = LagouTokenService.class)
public class LagouTokenServiceImpl implements LagouTokenService {

    @Autowired
    private LagouTokenDao lagouTokenDao;

    @Override
    public String saveToken(String email, String passWord){
        if(!StringUtils.isEmpty(email)){
            LagouToken userByEmail = findByEmail(email);
            if(Objects.nonNull(userByEmail)){
                lagouTokenDao.delete(userByEmail);
            }
        }
        String beforeEncode = Md5.getBeforeEncode(email, passWord);
        String afterEncode = Md5.encoderByMd5(beforeEncode);
        log.info("用户名{} 密码{} 映射成的token是{}", email, passWord, afterEncode);
        LagouToken lagouToken = LagouToken.builder()
                .token(afterEncode)
                .email(email)
                .build();
        lagouTokenDao.save(lagouToken);
        return afterEncode;
    }

    @Override
    public LagouTokenEntity findUserByEmail(String email) {

        LagouToken lagouToken1 = findByEmail(email);
        LagouTokenEntity res = new LagouTokenEntity();
        if(Objects.nonNull(lagouToken1)){
            BeanUtils.copyProperties(lagouToken1, res);
            return res;
        }
        return null;
    }

    @Override
    public LagouTokenEntity findUserByToken(String token) {
        if(StringUtils.isEmpty(token)){
            return null;
        }
        LagouTokenEntity res = new LagouTokenEntity();

        LagouToken lagouToken = LagouToken.builder()
                .token(token)
                .build();
        Example<LagouToken> example = Example.of(lagouToken);
        Optional<LagouToken> one = lagouTokenDao.findOne(example);
        if(one.isPresent()){
            BeanUtils.copyProperties(one.get(), res);
            return res;
        }
        return null;
    }

    private LagouToken findByEmail(String email){
        if(StringUtils.isEmpty(email)){
            return null;
        }
        LagouToken lagouToken = LagouToken.builder()
                .email(email)
                .build();
        Example<LagouToken> example = Example.of(lagouToken);
        Optional<LagouToken> one = lagouTokenDao.findOne(example);
        return one.orElse(null);
    }
}
